package com.unito.tableplus.shared.model;

import java.util.ArrayList;
//import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable = "true")
public class TableObject implements Resource {

	private static final long serialVersionUID = 5115867477108503053L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String key;
	@Persistent
	private String name;
	// MAGIO
	//@Persistent
	//private String title; // = null da bookmark
	@Persistent
	private String icon;
	@Persistent
	private String uri; // private String url
	@Persistent
	private Provider provider;
	@Persistent
	private ArrayList<String> tag;
	//private List<String> tag; MAGIO
	@Persistent
	private String type;
	@Persistent
	ArrayList<String> annotation; 
//private List<String> annotation; MAGIO
	@Persistent
	private String legend;
	// @Persistent
	// private String id;

	// @Persistent
	// private String user;
	@Persistent
	private User user; // da bookmark
	/**
	 * Used for mapping the resource with the table it belongs to.
	 */
	// @Persistent MAGIO  @Persistent(defaultFetchGroup = "true")
	 @Persistent
	private Table table;

	 
	@Persistent(mappedBy = "tableObject")
	//	@Persistent(mappedBy = "tableObject", defaultFetchGroup = "true")
	private ArrayList<Comment> commentList = new ArrayList<Comment>();

	// delete this and the sky will tumble and fall!!!
	public TableObject() {
		this.setAnnotation(new ArrayList<String>());
		this.setTag(new ArrayList<String>());
	}

	public TableObject(Resource resource, Table table) {
		this.setName(resource.getName());
		this.setIcon(resource.getIcon());
		this.setUri(resource.getURI());
		this.setProvider(resource.getProvider());
		// this.setId(resource.getId());
		this.setTable(table);
		this.setAnnotation(new ArrayList<String>());
		this.setTag(new ArrayList<String>());
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public String getIcon() {
		return this.icon;
	}

	@Override
	public String getURI() {
		return this.getUri();
	}

	@Override
	public Provider getProvider() {
		return this.provider;
	}

	@Override
	public void setURI(String uri) {
		this.setUri(uri);
	}

	public void setLegend(String legend) {
		this.legend = legend;
	}

	public void setAnnotation(ArrayList<String> annotation) {
		this.annotation = annotation;
	}
	/* 
	public void setAnnotation(List<String> annotation) {
		this.annotation = annotation;
	}
	public void setTagCategory(List<String> tag) {
		this.setTag(tag);
	} */

	public void setTagCategory(ArrayList<String> tag) {
		this.setTag(tag);
	}

	public ArrayList<String> getAnnotation() {
		return annotation;
	}

	/* 
	public List<String> getAnnotation() {
		return annotation;
	} */
	public int getAnnotationNumber() {
		return getAnnotation().size();
	}

	public ArrayList<String> getTag() {
		return tag;
	}

	public String getLegend() {
		return legend;
	}

	@Override
	public String toString() {
		return "SharedResource [key=" + getKey() + ", name=" + getName()
				+ ", icon=" + getIcon() + ", uri=" + getUri() + ", provider="
				+ getProvider() + ", id=" + ", table=" + getTable() + "]";
		// + getId() + ", table=" + getTable() + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((getIcon() == null) ? 0 : getIcon().hashCode());

		// result = prime * result + ((getId() == null) ? 0 :
		// getId().hashCode());
		result = prime * result
				+ ((getKey() == null) ? 0 : getKey().hashCode());
		result = prime * result
				+ ((getName() == null) ? 0 : getName().hashCode());
		result = prime * result
				+ ((getProvider() == null) ? 0 : getProvider().hashCode());
		result = prime * result
				+ ((getTable() == null) ? 0 : getTable().hashCode());
		result = prime * result
				+ ((getUri() == null) ? 0 : getUri().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TableObject other = (TableObject) obj;
		if (getIcon() == null) {
			if (other.getIcon() != null)
				return false;
		} else if (!getIcon().equals(other.getIcon()))
			return false;
		/*
		 * MAGIO if (getId() == null) { if (other.getId() != null) return false;
		 * } else if (!getId().equals(other.getId())) return false;
		 */
		if (getKey() == null) {
			if (other.getKey() != null)
				return false;
		} else if (!getKey().equals(other.getKey()))
			return false;
		if (getName() == null) {
			if (other.getName() != null)
				return false;
		} else if (!getName().equals(other.getName()))
			return false;
		if (getProvider() != other.getProvider())
			return false;
		if (getTable() == null) {
			if (other.getTable() != null)
				return false;
		} else if (!getTable().equals(other.getTable()))
			return false;
		if (getUri() == null) {
			if (other.getUri() != null)
				return false;
		} else if (!getUri().equals(other.getUri()))
			return false;
		return true;
	}

	public ArrayList<Comment> getComments() {
		//public List<Comment> getComments() {
		return getCommentList();

	}

	public void setName(String name) {
		this.name = name;
	}
/* MAGIO
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
*/
	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	public void setTag(ArrayList<String> tag) {
		this.tag = tag;
	}
	/*public void setTag(List<String> tag) {
		this.tag = tag;
	}*/

	/*
	 * MAGIO 11-3-2013 public String getId() { return id; }
	 * 
	 * public void setId(String id) { this.id = id; }
	 */
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	public ArrayList<Comment> getCommentList() {
		return commentList;
	}

	public void setCommentList(ArrayList<Comment> commentList) {
		this.commentList = commentList;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	// from Bookmark
	public String getAnnotationString() {
		String listAnnotation = "";
		for (String a : getAnnotation())
			listAnnotation += a + ", ";
		if (listAnnotation.length() > 0)
			return listAnnotation.substring(0, listAnnotation.length() - 2);
		else
			return "No Annotation";
	}

	public void addTag(String string) {
		boolean existing = false;
		for (String s : this.getTag()) {
			if (s.equalsIgnoreCase(string))
				existing = true;
		}
		if (!existing)
			this.tag.add(string);
	}

	public void removeTag(int tag) {
		this.getTag().remove(tag);

	}

	public void addAnnotation(String value) {
		if (this.getAnnotation() == null) {
			System.out.println("ANNOTATION NULLA");
			this.setAnnotation(new ArrayList<String>());
		}
		this.getAnnotation().add(value);

	}

	public void removeAnnotation(int annotation) {
		this.getAnnotation().remove(annotation);
	}

	public void addComment(Comment comment) {
		getComments().add(comment);
	}

	public String getTagString() {
		String listTag = "";
		for (String t : tag)
			listTag += t + ", ";
		if (listTag.length() > 0)
			return listTag.substring(0, listTag.length() - 2);
		else
			return "No Tag";
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	/*
	 * public String getUser() { return user; }
	 * 
	 * public void setUser(String user) { this.user = user; }
	 */

}
